package cn.iocoder.yudao.module.system.controller.admin.ai;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.ai.vo.AiAdmissionCutoffScoreCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.ai.vo.AiAdmissionCutoffScorePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.ai.vo.AiAdmissionCutoffScoreRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ai.vo.AiAdmissionCutoffScoreUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.ai.AiAdmissionCutoffScoreConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.ai.AiAdmissionCutoffScoreDO;
import cn.iocoder.yudao.module.system.service.ai.AiAdmissionCutoffScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - AI录取分数线")
@RestController
@RequestMapping("/system/ai-admission-cutoff-score")
@Validated
public class AiAdmissionCutoffScoreController {

    @Resource
    private AiAdmissionCutoffScoreService aiAdmissionCutoffScoreService;

    @PostMapping("/create")
    @Operation(summary = "创建AI录取分数线")
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:create')")
    public CommonResult<Long> createAiAdmissionCutoffScore(@Valid @RequestBody AiAdmissionCutoffScoreCreateReqVO createReqVO) {
        return success(aiAdmissionCutoffScoreService.createAiAdmissionCutoffScore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI录取分数线")
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:update')")
    public CommonResult<Boolean> updateAiAdmissionCutoffScore(@Valid @RequestBody AiAdmissionCutoffScoreUpdateReqVO updateReqVO) {
        aiAdmissionCutoffScoreService.updateAiAdmissionCutoffScore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI录取分数线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:delete')")
    public CommonResult<Boolean> deleteAiAdmissionCutoffScore(@RequestParam("id") Long id) {
        aiAdmissionCutoffScoreService.deleteAiAdmissionCutoffScore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI录取分数线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:query')")
    public CommonResult<AiAdmissionCutoffScoreRespVO> getAiAdmissionCutoffScore(@RequestParam("id") Long id) {
        AiAdmissionCutoffScoreDO aiAdmissionCutoffScore = aiAdmissionCutoffScoreService.getAiAdmissionCutoffScore(id);
        return success(AiAdmissionCutoffScoreConvert.INSTANCE.convert(aiAdmissionCutoffScore));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI录取分数线分页")
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:query')")
    public CommonResult<PageResult<AiAdmissionCutoffScoreRespVO>> getAiAdmissionCutoffScorePage(@Valid AiAdmissionCutoffScorePageReqVO pageReqVO) {
        PageResult<AiAdmissionCutoffScoreDO> pageResult = aiAdmissionCutoffScoreService.getAiAdmissionCutoffScorePage(pageReqVO);
        return success(AiAdmissionCutoffScoreConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI录取分数线 Excel")
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAiAdmissionCutoffScoreExcel(@Valid AiAdmissionCutoffScorePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiAdmissionCutoffScoreDO> list = aiAdmissionCutoffScoreService.getAiAdmissionCutoffScorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI录取分数线.xls", "数据", AiAdmissionCutoffScoreRespVO.class,
                        AiAdmissionCutoffScoreConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-by-school-name")
    @Operation(summary = "根据院校名称获得AI录取分数线列表")
    @Parameter(name = "schoolName", description = "院校名称", required = true)
    @Parameter(name = "province", description = "省份", required = true)
    @Parameter(name = "batchName", description = "批次名称", required = false)
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:query')")
    public CommonResult<List<AiAdmissionCutoffScoreRespVO>> getAiAdmissionCutoffScoreListBySchoolName(
            @RequestParam("schoolName") String schoolName,
            @RequestParam("province") String province,
            @RequestParam(value = "batchName", required = false) String batchName) {
        List<AiAdmissionCutoffScoreDO> list = aiAdmissionCutoffScoreService.getAiAdmissionCutoffScoreListBySchoolName(schoolName, province, batchName);
        return success(AiAdmissionCutoffScoreConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-by-school-name-and-type")
    @Operation(summary = "根据院校名称和科目类型获得AI录取分数线列表")
    @Parameter(name = "schoolName", description = "院校名称", required = true)
    @Parameter(name = "type", description = "科目类型", required = true)
    @Parameter(name = "province", description = "省份", required = true)
    @Parameter(name = "batchName", description = "批次名称", required = false)
    @PreAuthorize("@ss.hasPermission('system:ai-admission-cutoff-score:query')")
    public CommonResult<List<AiAdmissionCutoffScoreRespVO>> getAiAdmissionCutoffScoreListBySchoolNameAndType(
            @RequestParam("schoolName") String schoolName,
            @RequestParam("type") String type,
            @RequestParam("province") String province,
            @RequestParam(value = "batchName", required = false) String batchName) {
        List<AiAdmissionCutoffScoreDO> list = aiAdmissionCutoffScoreService.getAiAdmissionCutoffScoreListBySchoolNameAndType(schoolName, type, province, batchName);
        return success(AiAdmissionCutoffScoreConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/sync-school-uuid")
    @Operation(summary = "同步院校UUID", description = "从college表匹配并更新AI录取分数线表的schoolUuid字段")
    public CommonResult<String> syncSchoolUuid() {
        String result = aiAdmissionCutoffScoreService.syncSchoolUuid();
        return success(result);
    }

}